# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: jshin

from coin.exchange.base.order_gateway import OrderSide, OrderType
from coin.exchange.bitmex.kr_rest.futures_product import \
  BitmexFuturesProduct
from coin.exchange.bitmex.order_gateway import BitmexOrderGateway
from coin.strategy.executor.passive.simple_mm_executor import \
  SimpleMMExecutor
from coin.strategy.executor.util.traits import (order_logger, FuturesExecutorTraits)
from coin.strategy.executor.aggressive.aggressive_executor import AggressiveExecutor


class BitmexSimpleTraits(FuturesExecutorTraits):
  BuyOrderSide = [OrderSide.BUY]
  SellOrderSide = [OrderSide.SELL]

  def _check_state_impl(self):
    pass

  def _get_position(self, product):
    return self.og.get_position(product)

  @order_logger
  def _submit_order(self, product, price, qty, order_side):
    if not self.og.submittable():
      self._logger.warning('Skip submission: %s %f@%f' % (product, qty, price))
      return False
    self.og.submit(product, price, qty, order_side, OrderType.LIMIT)

  @order_logger
  def _submit_pass_order(self, product, price, qty, order_side):
    if not self.og.submittable():
      self._logger.warning('Skip submission: %s %f@%f' % (product, qty, price))
      return False
    self.og.submit(product,
                   price,
                   qty,
                   order_side,
                   OrderType.LIMIT,
                   post_only=self.config.post_only)

  def _amend_order(self, order_id, product, price, qty, order_side):
    self.logger.info('Amend order(%s): %s %s %s @ %s',
                     order_id,
                     order_side.name,
                     product,
                     qty,
                     price)
    self.og.amend(order_id, price, qty)

  def _cancel_orders_impl(self, order_id_list):
    self.og.cancel_multiple(order_id_list)


class BitmexSimpleMMExecutor(SimpleMMExecutor):
  ProductType = BitmexFuturesProduct
  OGType = BitmexOrderGateway
  Traits = BitmexSimpleTraits


class BitmexSimpleAggExecutor(AggressiveExecutor):
  ProductType = BitmexFuturesProduct
  OGType = BitmexOrderGateway
  Traits = BitmexSimpleTraits
